<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class SiteSettingForm extends MiniyunViewForm
{
    public  $siteName;    public  $siteTitle;      public  $siteLogoUrl;    public  $defaultSpace;    public  $company;    public  $siteLogoFile;    public  $siteLogoSmallFile;    public  $enabledReigster;    public  $registerStatusArray = array("1" => '开放注册',"0" => '禁止外部用户注册');
    private $_logoRootPath;    public  $success;    public function init(){
        $this->siteName    = Yii::app()->params['app']['name'];
        $this->siteTitle   = Yii::app()->params['app']['title'];
        $this->siteLogoUrl = Yii::app()->params['app']['logo'];
        $this->defaultSpace = Yii::app()->params['app']['defaultSpace'];
        $this->company      = Yii::app()->params['app']['company'];
        $this->enabledReigster = Yii::app()->params['app']['enabledReigster']?"1":"0";
        $this->_logoRootPath = dirname(__FILE__)."/../../../statics/";
        $this->success = false;
    }
    public function rules()
    {
        $file = CUploadedFile::getInstance($this,'siteLogoFile');
        if (!empty($file)){
            $this->siteLogoFile = $file;
        }
        $file = CUploadedFile::getInstance($this,'siteLogoSmallFile');
        if (!empty($file)){
            $this->siteLogoSmallFile = $file;
        }
        $valid = array(
        array('siteName, defaultSpace,company,siteTitle', 'required'),
        array('siteName', 'checkName'),
        array('siteTitle', 'checkTitle'),
        array('defaultSpace', 'numerical'),
        array('enabledReigster', 'checkEnabledReigster'),
        );

        if (isset($this->siteLogoFile)){
            $siteLogoFileValid = array('siteLogoFile', 'file',
                'types'=>'jpg,png',
                'maxSize'=>500 * 1024,                 'tooLarge'=>'站点LOGO超过500字节.',
            );
            array_push($valid, $siteLogoFileValid);
        }
        if (isset($this->siteLogoSmallFile)){
            $siteLogoSmallFileValid = array('siteLogoSmallFile', 'file',
                'types'=>'jpg,png',
                'maxSize'=>500*1024,                 'tooLarge'=>'站点小LOGO文件超过500字节.',
            );
            array_push($valid, $siteLogoSmallFileValid);
        }
        return $valid;
    }

    
    public function checkTitle() {
        $str = $this->siteTitle;
        $length = CCharacter::utf8Strlen($str);
        if ($length>40) {
            $this->addError('siteTitle','站点标题必须小于20字');
            return;
        }
        return true;
    }

    
    public function checkName() {
        $str = $this->siteName;
        $length = CCharacter::utf8Strlen($str);
        if ($length>10) {
            $this->addError('siteName','站点名称必须中文不大于5字,英文不大于10字');
            return;
        }
        return true;
    }

    
    public function checkEnabledReigster(){
        if(isset($this->enabledReigster)){
            if($this->enabledReigster=="1" || $this->enabledReigster=="0"){
                return true;
            }
        }
        $this->addError("enabledReigster", "开放注册选项设置不正确 ");
        return false;
    }

    
    private function _saveLogoFile($file,$name){
                list($width, $height, $type, $attr) = getimagesize($file->getTempName());
        if($width!=256 || $height!=256){
            $this->addError("siteLogoFile", "站点LOGO像素不是256*256,请检查");
            return false;
        }
        $savePath = $this->_logoRootPath."/logo/";
        if(file_exists($savePath)==false){
            mkdir($savePath);
        }
        $file->saveAs($savePath.$name);
        return true;
    }

    
    public function attributeLabels()
    {
        return  array(
            'siteName'=>'站点名称',
            'siteTitle'=>'站点标题',
            'siteLogoUrl'=>'站点LOGO',
            'defaultSpace'=>'用户默认空间',
            'fileStorePath'=>'文件存储位置',
            'company'=>'公司',
		    'enabledReigster'=>'用户注册',
		    );
    }
    public function save(){
        if($this->validate()){
            $file = CUploadedFile::getInstance($this,'siteLogoFile');
            if (!empty($file)){
                $name = $file->name;
                $newName = $this->generateFileName($name);
                if($this->_saveLogoFile($file,$newName)){                    $this->siteLogoUrl = "/statics/logo/".$newName;
                }else{
                    return false;
                }
            }

                        $this->_saveOption("site_title",$this->siteTitle);
            $this->_saveOption("site_name",$this->siteName);
            $this->_saveOption("site_logo_url",$this->siteLogoUrl);
            $this->_saveOption("site_logo_small_url",$this->siteLogoUrl);
            $this->_saveOption("site_default_space",$this->defaultSpace);
            $this->_saveOption("site_company",$this->company);
            $this->_saveOption("user_register_enabled",$this->enabledReigster);
            $this->success = true;
            unset($_SESSION['appOptions']);             return true;
             
        }
    }
    private  function _saveOption($key,$value){
        $option = Option::model()->find("option_name=?",array($key));
        if(!isset($option)){
            $option = new Option();
            $option["option_name"]=$key;
        }
        $option["option_value"]=$value;
        $option->save();
    }
}